/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.tenant.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.model.enums.DeletedEnum;
import net.siufung.core.result.Result;
import net.siufung.core.result.ResultFail;
import net.siufung.core.result.ResultOk;
import net.siufung.core.utils.CopyUtil;
import net.siufung.market.base.tenant.controller.assembler.TenantQuotaAssembler;
import net.siufung.market.base.tenant.controller.model.dto.TenantQuotaSaveDto;
import net.siufung.market.base.tenant.controller.model.dto.TenantQuotaUpdateDto;
import net.siufung.market.base.tenant.controller.model.vo.TenantQuotaVo;
import net.siufung.market.base.tenant.controller.regulation.TenantQuotaCreateBatchRegulation;
import net.siufung.market.base.tenant.controller.regulation.TenantQuotaCreateRegulation;
import net.siufung.market.base.tenant.controller.regulation.TenantQuotaStatusRegulation;
import net.siufung.market.base.tenant.controller.regulation.TenantQuotaUpdateRegulation;
import net.siufung.market.base.tenant.mapper.ITenantQuotaMapper;
import net.siufung.market.base.tenant.mapper.entity.TenantQuota;
import net.siufung.market.base.tenant.service.ITenantQuotaService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 系统租户配额表 前端控制器
 *
 * @author 陈建峰
 * @since 2022-07-06 14:45:08
 */
@Api(tags = "系统租户配额表")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/tenant/quota")
public class TenantQuotaController {

    private final ITenantQuotaMapper tenantQuotaMapper;
    private final ITenantQuotaService tenantQuotaService;

    /**
     * 通过主键获取 系统租户配额表
     * @param id 主键
     * @return Result<TenantQuotaVo>
     * */
    @ApiOperation(value = "通过Id获取系统租户配额表信息")
    @GetMapping(value = "/id/{id}")
    public Result<TenantQuotaVo> getById(
            @PathVariable("id") Integer id) {
        return Result.ok(
            TenantQuotaAssembler.from(tenantQuotaMapper.selectById(id)));
    }

    /**
     * 获取 系统租户配额表列表
     * @param tenantId 属性
     * @return Result<List<TenantQuotaVo>>
     * */
    @ApiOperation(value = "获取系统租户配额表列表")
    @PostMapping(value = "/list/{tenantId}")
    public Result<List<TenantQuotaVo>> list(
            @PathVariable("tenantId") Integer tenantId) {
        return Result.ok(
            TenantQuotaAssembler.fromList(
                    tenantQuotaMapper.selectList(
                            new LambdaQueryWrapper<TenantQuota>()
                                    .eq(TenantQuota::getTenantId, tenantId))));
    }

    /**
     * 通过主键删除 系统租户配额表 对象
     * @param id 主键
     * @return 删除结果
     * */
    @ApiOperation(value = "删除系统租户配额表对象")
    @PostMapping(value="/delete/{id}")
    public Result<Void> deleteById(
        @PathVariable("id") Integer id) {
        if(tenantQuotaService.removeById(id)){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 通过主键集合批量删除 系统租户配额表
     * @param ids 主键集合
     * @return 批量删除结果
     * */
    @ApiOperation(value = "批量删除系统租户配额表对象")
    @PostMapping(value="/deleteBatch")
    public Result<Void> deleteBatch(@RequestBody Integer[] ids) {
        if(tenantQuotaService.removeByIds(Arrays.asList(ids))){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 创建 系统租户配额
     * @param tenantQuotaSaveDto 对象属性
     * @return 创建结果
     * */
    @ApiOperation(value = "创建 系统租户配额")
    @PostMapping(value="/create")
    public Result<Void> create(
            @RequestBody TenantQuotaSaveDto tenantQuotaSaveDto) {
        tenantQuotaSaveDto.validated();
        TenantQuota tenantQuota = tenantQuotaSaveDto.to();
        TenantQuotaCreateRegulation tenantQuotaCreateRegulation = new TenantQuotaCreateRegulation(tenantQuotaService);
        tenantQuotaCreateRegulation.isSatisfiedBy(tenantQuota);
        if(tenantQuotaService.save(tenantQuota)){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }

    /**
     * 批量创建 系统租户配额
     * @param tenantQuotaSaveDtoList 对象属性
     * @return 创建结果
     * */
    @ApiOperation(value = "批量创建 系统租户配额")
    @PostMapping(value="/createBatch")
    public Result<Void> createBatch(
            @RequestBody List<TenantQuotaSaveDto> tenantQuotaSaveDtoList) {
        TenantQuota tenantQuota;
        List<TenantQuota> tenantQuotaList = Lists.newArrayList();
        for (TenantQuotaSaveDto tenantQuotaSaveDto: tenantQuotaSaveDtoList) {
            tenantQuotaSaveDto.validated();
            tenantQuota = tenantQuotaSaveDto.to();
            tenantQuotaList.add(tenantQuota);
        }
        TenantQuotaCreateBatchRegulation tenantQuotaCreateBatchRegulation = new TenantQuotaCreateBatchRegulation(tenantQuotaService);
        tenantQuotaCreateBatchRegulation.isSatisfiedBy(tenantQuotaList);
        if(tenantQuotaService.saveBatch(tenantQuotaList)){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }

    /**
     * 更新 系统租户配额表
     * @param tenantQuotaUpdateDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 系统租户配额表 信息")
    @PostMapping(value="/updateById")
    public Result<Void> updateById(
            @RequestBody TenantQuotaUpdateDto tenantQuotaUpdateDto) {
        tenantQuotaUpdateDto.validated();
        TenantQuota tenantQuotaOf = tenantQuotaService.getById(tenantQuotaUpdateDto.getId());
        CopyUtil.copyPropertiesIgnoreNull(tenantQuotaUpdateDto.to(), tenantQuotaOf);
        TenantQuotaUpdateRegulation tenantQuotaUpdateRegulation = new TenantQuotaUpdateRegulation(tenantQuotaService);
        tenantQuotaUpdateRegulation.isSatisfiedBy(tenantQuotaOf);
        if(tenantQuotaService.updateById(tenantQuotaOf)){
            return Result.ok(ResultOk.UPDATE);
        }
        return Result.fail(ResultFail.DATA_UPDATE_ERROR);
    }

    /**
     * 更新 系统租户配额表 状态
     * @param id 主键
     * @param deleted 数据状态
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 系统租户配额表 状态")
    @PostMapping(value="/updateStatusById/{id}/{deleted}")
    public Result<Void> updateStatusById(
            @PathVariable("id") Integer id,
            @PathVariable("deleted") boolean deleted) {
        TenantQuotaStatusRegulation tenantQuotaStatusRegulation = new TenantQuotaStatusRegulation(tenantQuotaMapper);
        tenantQuotaStatusRegulation.isSatisfiedBy(id);
        if(tenantQuotaService.updateStatusById(id, DeletedEnum.get(deleted))){
            return Result.ok(ResultOk.STATUS);
        }
        return Result.fail(ResultFail.DATA_UPDATE_ERROR);
    }

}